<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('在线前台用户列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="online-form">
                <div class="select-list">
                    <ul>
                        <li>
                            登录地址：<input type="text" name="ipaddr"/>
                        </li>
                        <li>
                            登录名称：<input type="text" name="loginName"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-danger multiple disabled" onclick="javascript:batchForceLogout()">
                <i class="fa fa-sign-out"></i> 强退
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-table-export-js" />
<script th:inline="javascript">
    var forceFlag = [[${@permission.hasPermi('monitor:online:forceLogout')}]];
    var turl = "/monitor/online/queryAllTokenSession";
    $(function() {
        var options = {
            uniqueId: "id",
            url: turl,
            sortName: "createTime",
            sortOrder: "desc",
            showExport: true,
            escape: true,
            method:"get",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '会话编号',
                    formatter: function(value, row, index) {
                      return value.toString().substring(28,value.toString().length)
                    }
                },
                {
                 field:'dataMap.userInfo.userId',
                    title:'用户编号'
                },

                {

                },
                {
                    field: 'dataMap.userInfo.loginName',
                    title: '登录名称',
                    sortable: true
                },
                {
                    field: 'dataMap.deptInfo.deptName',
                    title: '部门名称'
                },
                {
                    field: 'dataMap.userInfo.loginDate',
                    title: '登录时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var msg = '<a class="btn btn-danger btn-xs ' + forceFlag + '" href="javascript:void(0)" onclick="forceLogout(\'' + row.dataMap.userInfo.userId + '\')"><i class="fa fa-sign-out"></i>强退</a> ';
                        return msg;
                    }
                }]
        };
        $.table.init(options);
    });

    // 单条强退
    function forceLogout(userId) {
        console.log(userId);
        $.modal.confirm("确定要强制选中用户下线吗？", function() {
            var url ="http://localhost:1001/loginServer/sso/batchLogout";
            var data = [userId*1];
            $.ajax(
                {
                    url:url,
                    headers:{
                      satoken:getCookie("satoken")
                    },
                    contentType:"application/json",
                    data:JSON.stringify(data),
                    dataType:"json",
                    method:"post",
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.ajaxSuccess(result);
                    }
                }
            )
        })
    }

    // 批量强退
    function batchForceLogout() {
        var rows = $.table.selectColumns("dataMap.userInfo.userId");
        if (rows.length == 0) {
            $.modal.alertWarning("请选择要强退的用户");
            return;
        }
        postData = [];
        $.modal.confirm("确认要强退选中的" + rows.length + "条数据吗?", function() {
            var url ="http://localhost:1001/loginServer/sso/batchLogout";
            $.ajax(
                {
                    url:url,
                    contentType:"application/json",
                    data:JSON.stringify(rows),
                    dataType:"json",
                    method:"post",
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                    },
                    success: function(result) {
                        if (typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.ajaxSuccess(result);
                    }
                }
            )
        });
    }
    function getCookie(cookie_name) {
        var allcookies = document.cookie;
        //索引长度，开始索引的位置
        var cookie_pos = allcookies.indexOf(cookie_name);


        // 如果找到了索引，就代表cookie存在,否则不存在
        if (cookie_pos != -1) {
            // 把cookie_pos放在值的开始，只要给值加1即可
            //计算取cookie值得开始索引，加的1为“=”
            cookie_pos = cookie_pos + cookie_name.length + 1;
            //计算取cookie值得结束索引
            var cookie_end = allcookies.indexOf(";", cookie_pos);


            if (cookie_end == -1) {
                cookie_end = allcookies.length;


            }
            //得到想要的cookie的值
            var value = unescape(allcookies.substring(cookie_pos, cookie_end));
        }
        return value;
    }
</script>
</body>
</html>